﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Collections.ObjectModel;
using System.Data.SqlClient;
using TracNghiemTinHocDaiCuong.Common;
using TracNghiemTinHocDaiCuong.ThucThe;

namespace TracNghiemTinHocDaiCuong.DAO
{
    public class DeThi_CauHoiDAO
    {
        public static DeThi_CauHoi DoiTuong(IDataRecord read)
        {
            return new DeThi_CauHoi
            {
                MaDT = Methods.ConvertNumber(read["MaDT"].ToString()),
                MaCH = Methods.ConvertNumber(read["MaCH"].ToString()),
                GhiChu = Methods.ConvertString(read["GhiChu"].ToString())
            };
        }

        public static IList<DeThi_CauHoi> LayDanhSach()
        {
            IList<DeThi_CauHoi> danhSach = new List<DeThi_CauHoi>();
            string query = string.Format("SELECT * FROM DeThi_CauHoi");
            IDataReader read = SqlHelper.ExecuteReader(query);
            while(read.Read())
            {
                danhSach.Add(DoiTuong(read));
            }
            return danhSach;
        }

        public static bool Them(DeThi_CauHoi deCauHoi)
        {
            string query = string.Format("INSERT INTO DeThi_CauHoi(MaDT, MaCH, GhiChu) VALUES(@MaDT, @MaCH, @GhiChu)");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@MaDT", deCauHoi.MaDT),
                new SqlParameter("@MaCH", deCauHoi.MaCH),
                new SqlParameter("@GhiChu", deCauHoi.GhiChu)
            };
            SqlHelper.ExecuteNonQuery(query, parameter);
            return true;
        }

        public static bool Sua(DeThi_CauHoi deCauHoi)
        {
            string query = string.Format("UPDATE DeThi_CauHoi SET MaCH=@MaCH, GhiChu=@GhiChu WHERE MaDT=@MaDT");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@MaDT", deCauHoi.MaDT),
                new SqlParameter("@MaCH", deCauHoi.MaCH),
                new SqlParameter("@GhiChu", deCauHoi.GhiChu)
            };
            SqlHelper.ExecuteNonQuery(query, parameter);
            return true;
        }

        public static bool Xoa(DeThi_CauHoi deCauHoi)
        {
            string query = string.Format("DELETE FROM DeThi_CauHoi WHERE MaDT=@MaDT, MaCH=@MaCH");
            var parameter = new Collection<SqlParameter>
            {
                new SqlParameter("@MaDT", deCauHoi.MaDT),
                new SqlParameter("@MaCH", deCauHoi.MaCH)
            };
            SqlHelper.ExecuteNonQuery(query, parameter);
            return true;
        }
    }
}